<template>
    <div>
        <div class="page-bg"></div>
        <div class="standard-wrap">
            <div v-for="(item,index) in  listData"
                 v-bind:key="index"
                 class="item-wrap">
                <standardItem :item="item"
                              :showChild="showChild"
                              :selectStandard="selectStandard"></standardItem>
            </div>
        </div>
        <div class="affirm-wrap">
            <button class="affirm"
                    @click="affirm">确定</button>
        </div>
        <myMenu></myMenu>
    </div>
</template>
<script>
import standardItem from './standardItem'
import myMenu from '@/commonComponents/menu'
export default {
    data () {
        return {
            listData: [],
            childData: [],
            userId: '',
            tokenId: '',
            projectId: '',
            userInfo: ''
        }
    },
    components: {
        standardItem,
        myMenu
    },
    created () {
        this.userInfo = JSON.parse(localStorage.userInfo)
        this.userId = this.userInfo.userid
        this.tokenId = this.userInfo.tokenId
        this.projectId = this.userInfo.deptid
        this.getData()
    },
    methods: {
        showChild (deptId, item) {
            item.open = !item.open
        },
        selectStandard (item) {
            item.checked = !item.checked
        },
        getData () {
            let params = {
                'allowPaging': true,
                'business': 'FetchHiddenDangerStandards',
                'data': {
                    'ProjectId': this.projectId,
                    'id': this.$route.query.id ? this.$store.state.hiddenlist1[this.$route.query.id].categoryId : this.$store.state.hiddenlist.categoryId
                },
                'pageIndex': 0,
                'pageSize': 999,
                'tokenId': this.tokenId,
                'userId': this.userId
            }
            if (this.$store.state.localId) {
                params.data.id = this.$store.state.hiddenlistLocalId[this.$store.state.localId].categoryId
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    debugger
                    res.data.data.forEach(item => {
                        item.open = false

                        if (item.type === 0) {
                            this.listData.push(item)
                        } else {
                            item.checked = false
                            this.childData.push(item)
                        }
                    })
                    this.childData.forEach(item => {
                        this.dataRecursion(this.listData, item)
                    })
                }
            }).catch(() => {

            })
        },
        dataRecursion (dataArr, item) {
            for (let i = 0; i < dataArr.length; i++) {
                if (dataArr[i].DeptId === item.ParentId) {
                    if (dataArr[i].child instanceof Array) {
                        dataArr[i].child.push(item)
                    } else {
                        dataArr[i].child = []
                        dataArr[i].child.push(item)
                    }
                    return false
                }
                if (dataArr[i].child && dataArr[i].child.length > 0) {
                    this.dataRecursion(dataArr[i].child, item)
                }
            }
        },
        affirm () {
            let standardId = ''
            let standardStr = ''
            this.childData.forEach(item => {
                if (item.checked) {
                    if (standardStr) {
                        standardStr += `${item.DeptName}`
                        standardId += `,${item.DeptId}`
                    } else {
                        standardStr += `${item.DeptName}`
                        standardId += `${item.DeptId}`
                    }
                }
            })
            if (this.$route.query.id) {
                this.$store.state.hiddenlist1[this.$route.query.id] = Object.assign({}, this.$store.state.hiddenlist1[this.$route.query.id], {
                    'standardId': standardId,
                    'description': standardStr
                })
            } else {
                if (this.$store.state.localId) {
                    this.$store.state.hiddenlistLocalId[this.$store.state.localId] = Object.assign({}, this.$store.state.hiddenlistLocalId[this.$store.state.localId], {
                        'standardId': standardId,
                        'description': standardStr
                    })
                } else {
                    this.$store.state.hiddenlist = Object.assign({}, this.$store.state.hiddenlist, {
                        'standardId': standardId,
                        'description': standardStr
                    })
                }
            }
            localStorage.setItem('standardStr', standardStr)
            // localStorage.setItem('standardId', )

            // this.$router.go(-1)
            if (this.$route.query.id) {
                this.$router.push({
                    path: '/checkdec',
                    query: {
                        id: this.$route.query.id
                    }
                })
            } else {
                this.$router.push('/checkdec')
            }
        }
    }
}
</script>
<style scoped>
.page-bg {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    background: #ebeef2;
}
.standard-wrap {
    padding-bottom: 60px;
}
.affirm-wrap {
    position: fixed;
    z-index: 5;
    bottom: 0;
    left: 0;
    right: 0;
    background: #ebeef2;
}
.item-wrap {
    padding-left: 20px;
    background: #fff;
    position: relative;
    z-index: 2;
    margin-bottom: 5px;
}
.affirm {
    position: relative;
    z-index: 2;
    display: block;
    width: 200px;
    height: 40px;
    border-radius: 20px;
    background: #2269ff;
    color: #fff;
    font-size: 15px;
    margin: 10px auto;
}
</style>
